﻿using System;

using Top.Api;
using Top.Api.Parser;
using Top.Api.Domain;
using Top.Api.Request;
using Top.Api.Response;

using DreamCube.Foundation.Basic.Utility;

namespace DreamCube.TaobaoGJ.BLL.Api
{
    /// <summary>
    /// 淘宝店铺API
    /// </summary>
    public class DP_Api
    {
        /// <summary>
        /// taobao.sellercats.list.add 添加卖家自定义类目
        /// </summary>
        /// <param name="sellerCat"></param>
        /// <returns></returns>
        public static Boolean Add_List_Sellercats(SellerCat sellerCat, out String responseXml)
        {
            responseXml = String.Empty;
            try
            {
                ITopClient client = TB_AppKeyFunc.GetTopClient();
                SellercatsListAddRequest req = new SellercatsListAddRequest();
                req.Name = sellerCat.Name;
                req.PictUrl = sellerCat.PicUrl;
                req.ParentCid = sellerCat.ParentCid;
                req.SortOrder = sellerCat.SortOrder;
                SellercatsListAddResponse response = client.Execute(req, SessionKey.GetSessionKey());
                if (response.IsError)
                {
                    ApiCommon.MakeLog("DP_Api", "Add_List_Sellercats()", response);
                    return false;
                }
                responseXml = response.Body;
                return true;
            }
            catch (Exception ex)
            {
                MyLog.MakeLog(ex);
            }
            return false;
        }

        /// <summary>
        /// 根据卖家名称，获取卖家所有的【店铺内卖家自定义类目】
        /// 此API获取当前卖家店铺在淘宝前端被展示的浏览导航类目（面向买家） 
        /// (taobao.sellercats.list.get)
        /// </summary>
        /// <param name="nick"></param>
        /// <returns></returns>
        public static String Get_List_Sellercats(String nick, String sessionKey = "")
        {
            try
            {
                ITopClient client = TB_AppKeyFunc.GetTopClient();
                SellercatsListGetRequest req = new SellercatsListGetRequest();
                req.Nick = nick;
                SellercatsListGetResponse response = client.Execute(req, sessionKey);
                if (response.IsError)
                {
                    ApiCommon.MakeLog("DP_Api", "Get_List_Sellercats()", response);
                    return String.Empty;
                }
                return response.Body;
            }
            catch (Exception ex)
            {
                MyLog.MakeLog(ex);
                return String.Empty;
            }
        }

        /// <summary>
        /// (taobao.shopcats.list.get)
        /// 根据卖家名称，获取卖家所有的【店铺类目】
        /// 获取淘宝面向买家的浏览导航类目（跟后台卖家商品管理的类目有差异） 
        /// </summary>
        /// <param name="fields"></param>
        /// <returns></returns>
        public static String Get_List_Shopcats(String fields = "")
        {
            try
            {
                ITopClient client = TB_AppKeyFunc.GetTopClient();
                ShopcatsListGetRequest req = new ShopcatsListGetRequest();
                if(String.IsNullOrEmpty(fields)) fields = Properties.Resources.TB_ShopCat_Field;
                req.Fields = fields;
                ShopcatsListGetResponse response = client.Execute(req);
                if (response.IsError)
                {
                    ApiCommon.MakeLog("DP_Api", "Get_List_Shopcats()", response);
                    return null;
                }
                return response.Body;
            }
            catch (Exception ex)
            {
                MyLog.MakeLog(ex);
                return null;
            }
        }
    }
}
